package com.tsd.base.controller;

import com.alibaba.fastjson.JSON;
import com.tsd.base.entity.BaseAssistType;
import com.tsd.base.entity.BaseAssistTypeExt;
import com.tsd.base.service.BaseAssistTypeService;
import com.tsd.core.utils.HlpUtils;
import com.tsd.core.vo.ErrorCode;
import com.tsd.core.vo.HttpResult;
import com.tsd.system.controller.SystemBaseController;
import com.tsd.system.entity.SysUser;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 描述：辅助数据分类 控制层
 *
 * @author zsy
 * @date 2020/12/29 15:22:59
 */
@Controller
@Scope("prototype")
@RequestMapping("/api/v2")
public class BaseAssistTypeController extends SystemBaseController {

    @Resource
    private BaseAssistTypeService baseAssistTypeService;

    /**
     * 根据ID获取辅助数据分类
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/getBaseAssistTypeById.action", method = RequestMethod.POST)
    public HttpResult<BaseAssistType> getBaseAssistTypeById() {
        HttpResult<BaseAssistType> hr = new HttpResult<BaseAssistType>();
        try {
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            BaseAssistType baseAssistType = baseAssistTypeService.getBaseAssistTypeById(id);
            hr.setData(baseAssistType);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 根据id删除删除辅助数据分类
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/deleteBaseAssistType.action", method = RequestMethod.POST)
    public HttpResult<String> deleteBaseAssistType() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            SysUser opUser = super.getSysUser();
            Map<String, Object> map = super.getParams(request);
            Long id = HlpUtils.getLong(map, "id");
            super.checkEmpty(id, "请提供参数id");
            baseAssistTypeService.deleteBaseAssistType(id, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }


    /**
     * 保存辅助数据分类
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/saveBaseAssistType.action", method = RequestMethod.POST)
    public HttpResult<String> saveBaseAssistType() {
        HttpResult<String> hr = new HttpResult<String>();
        try {
            Map<String, Object> map = super.getParams(request);
            SysUser opUser = super.getSysUser();
            String json = JSON.toJSONString(map);
            BaseAssistType t = JSON.parseObject(json, BaseAssistType.class);
            baseAssistTypeService.saveBaseAssistType(t, opUser);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

    /**
     * 不分页查询辅助数据分类
     *
     * @return
     */
    @ResponseBody
    @RequestMapping(value = "/findBaseAssistTypes.action", method = RequestMethod.POST)
    public HttpResult<List<BaseAssistTypeExt>> findBaseAssistTypes() {
        HttpResult<List<BaseAssistTypeExt>> hr = new HttpResult<List<BaseAssistTypeExt>>();
        try {
            Map<String, Object> map = super.getParams(request);
            Map<String, Object> params = new HashMap<String, Object>();
            HlpUtils.copyIntegerValue(map, params, "dr", "dr", false, 0);
            List<BaseAssistTypeExt> list = baseAssistTypeService.findBaseAssistTypes(params);
            hr.setData(list);
            hr.setCode(ErrorCode.CODE_SUCCESS);
        } catch (Exception e) {
            super.procException(hr, e);
        }
        return hr;
    }

}
